package com.smartlink.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;

import com.opensymphony.xwork2.ActionSupport;
import com.smartlink.pojo.Building;
import com.smartlink.pojo.CT;
import com.smartlink.pojo.Classroom;
import com.smartlink.service.BuildingService;
import com.smartlink.service.ClassroomService;

public class ClassroomAction extends ActionSupport implements RequestAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Map<String, Object> request;
	private BuildingService buService;
	private ClassroomService classroomService;

	public ClassroomAction() {
		super();
		// TODO Auto-generated constructor stub
	}

	private int currentIndex = 1;// 分页标记 暂时为节省时间 没有实现
	private String clsroomId;

	public String getClsroomId() {
		return clsroomId;
	}

	public void setClsroomId(String clsroomId) {
		this.clsroomId = clsroomId;
	}

	public String showAddClassroomForm() {
		request.put("buList", buService.getAll());
		return "SUCCESS";

	}

	public String addClassroom() {
		HttpServletRequest req = ServletActionContext.getRequest();
		String nameNumber = req.getParameter("nameNumber");
		String piIp = req.getParameter("piIp");
		String desc = req.getParameter("desc");
		String latlon1 = req.getParameter("latlon1");
		String latlon2 = req.getParameter("latlon2");
		String buildingId = req.getParameter("buildingId");
		Classroom classroom = new Classroom();
		setLatLon2Classroom(classroom, latlon1, latlon2);
		classroom.setNameNumber(nameNumber);
		classroom.setDescription(desc);
		classroom.setBuilding(new Building(Integer.parseInt(buildingId)));
		classroom.setPiIp(piIp);
		boolean flag = classroomService.upload(classroom);
		if (flag) {
			return "SUCCESS";
		} else {
			return "FAIL";
		}

	}

	public String removeClassroom() {
		boolean flag = classroomService.delete(clsroomId);
		if (flag) {
			return "SUCCESS";
		} else {
			return null;
		}

	}

	public String showClassroomList() {
		request.put("classroomList", classroomService.getList());
		return "SUCCESS";
	}

	public String showClassroom() {
		Classroom clsroom = classroomService.getByid(clsroomId);
		request.put("classroom", clsroom);
		if (clsroom != null) {
			return "SUCCESS";
		} else {
			return null;
		}

	}

	public String showCTList() {
		List<CT> ctList = classroomService.getCTList(clsroomId);
		request.put("ctList", ctList);
		request.put("clsroomId", clsroomId);
		return "SUCCESS";
	}

	public String removeAdmin() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ctId = request.getParameter("ctId");
		classroomService.removeCTItem(ctId);
		return "SUCCESS";
	}

	/**
	 * 分割经纬
	 * 
	 * @param cr
	 * @param latlon1
	 * @param latlon2
	 * @return
	 */
	private Classroom setLatLon2Classroom(Classroom cr, String latlon1, String latlon2) {
		// 先提交纬度lat 然后是lon进度
		String[] info1 = latlon1.split("-");
		String[] info2 = latlon2.split("-");
		cr.setLat1(Double.parseDouble(info1[0]));
		cr.setLon1(Double.parseDouble(info1[1]));
		cr.setLat2(Double.parseDouble(info2[0]));
		cr.setLon2(Double.parseDouble(info2[1]));
		return cr;

	}

	// ***********************自动生成方法区***********************************************************

	@Override
	public void setRequest(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		request = arg0;

	}

	public void setBuService(BuildingService buService) {
		this.buService = buService;
	}

	public void setClassroomService(ClassroomService classroomService) {
		this.classroomService = classroomService;
	}

	public int getCurrentIndex() {
		return currentIndex;
	}

	public void setCurrentIndex(int currentIndex) {
		this.currentIndex = currentIndex;
	}

}
